<template>
    <div class="app-container">
        <!-- 条件查询表单 -->
        <el-form :inline="true" class="demo-form-inline">
            <el-form-item>
                <el-input  v-model="searchObj.hosname" placeholder="医院名称"/>
            </el-form-item>
            <el-form-item>
                <el-input v-model="searchObj.hoscode" placeholder="医院编号"/>
            </el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="initHospitalList(1)">查询</el-button>
            </el-form>
            <!-- 批量删除按钮 -->
        <el-button type="danger" size="mini" @click="removeRows()">批量删除</el-button>
        <el-table :data="list" stripe style="width: 100%" @selection-change="batchRemove">
            
             <el-table-column type="selection" width="55"/>
            <el-table-column type="index" width="50"/>
            <el-table-column prop="hosname" label="医院名称"/>
            <el-table-column prop="hoscode" label="医院编号"/>
            <el-table-column prop="apiUrl" label="api基础路径" width="200"/>
            <el-table-column prop="contactsName" label="联系人姓名"/>
            <el-table-column prop="contactsPhone" label="联系人手机"/>
            <el-table-column label="状态" width="80">
                <template slot-scope="scope">
                        {{ scope.row.status === 1 ? '可用' : '不可用' }}
                </template>
         </el-table-column>
         <el-table-column label="操作" width="280" align="center">
            <template slot-scope="scope">
                <el-button type="danger" size="mini" 
                    icon="el-icon-delete" @click="removeHospSetById(scope.row.id,scope.row.hosname)"> </el-button>
                <el-button v-if="scope.row.status==1" type="primary" size="mini" 
                    icon="el-icon-delete" @click="lockHostSet(scope.row.id,0)">锁定</el-button>
                <el-button v-if="scope.row.status==0" type="danger" size="mini" 
                    icon="el-icon-delete" @click="lockHostSet(scope.row.id,1)">取消锁定</el-button>
                <router-link :to="'/hospset/hospitalset/'+scope.row.id">
                     <el-button type="primary" size="mini" icon="el-icon-edit"></el-button>
                </router-link>
            </template>
         </el-table-column>        
        </el-table>
           <!-- 分页条 -->
           <el-pagination
                @size-change="initHospitalList"
                @current-change="initHospitalList"
                :current-page.sync="current"
                :page-size="limit"
                 style="padding: 30px 0; text-align: center;"
                 layout="total, prev, pager, next"
                :total="this.total">
            </el-pagination>
    </div>
</template>

<script>
import hospSetApi from '@/api/hosp/hospitalSet'
export default{
    data(){
        return {
            limit:5,
            current:1,
            list:[],
            total:0,
            searchObj:{},
            batchRemoveList:[],
            hospSetName:[],
            ids:[]
        }
    },
    created(){
        this.initHospitalList(this.current);
    },
    methods:{
        // 初始化医院设置表格
        initHospitalList(page){
            hospSetApi.getPageList(page,this.limit,this.searchObj)
                .then(response =>{
                   this.list =  response.data.records
                   this.total = response.data.total
                })
        },
        // 根据id删除医院设置
        removeHospSetById(id,name){
           
                this.$confirm('此操作将永久删除【'+name+'】设置信息, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => { //确定执行then方法
                    hospSetApi.removeHospSet(id)
                    .then(response =>{
                        this.$message({
                            type: 'success',
                            message: '删除成功!'
                        })
                        this.initHospitalList(this.current)
                    })
                })

        },
        // checkbox 改变触发该事件
        batchRemove(selection){
            this.batchRemoveList = selection
        },
        // 批量删除按钮
        removeRows(){
            if(this.batchRemoveList.length ==0){
                return 
            }
            let batchRemoveList = this.batchRemoveList
            for(let i = 0;i<batchRemoveList.length;i++){
                this.hospSetName.push(batchRemoveList[i].hosname)
                this.ids.push(batchRemoveList[i].id)
            }
            this.$confirm('此操作将删除【'+this.hospSetName+'】等, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => { //确定执行then方法

                    hospSetApi.batchRemove(this.ids)
                    .then(response =>{
                        this.$message({
                            type: 'success',
                            message: '删除成功!'
                        })
                        this.initHospitalList(this.current)
                    })
                }).catch(error =>{
                    this.hospSetName =[]
                    this.ids = []
                })
            
        },
        lockHostSet(id,status){
            hospSetApi.lockHostSet(id,status)
                .then(response =>{
                    this.initHospitalList(this.current)
                })
        }
    }
    
}

</script>